package com.springboot.controller.blogview;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.config.log.SpringBootRequestLog;
import com.springboot.config.controller.BaseController;
import com.springboot.file.BaseFileStoreFileUtil;
import com.springboot.jwt.dto.RequestComm;
import com.springboot.jwt.interfaces.JwtToken;
import com.springboot.returndata.dto.blog.BlogHomeContentPageDto;
import com.springboot.returndata.vo.blog.*;
import com.springboot.service.blog.BlogArticleTagService;
import com.springboot.util.enums.RespErrorCode;
import com.springboot.util.http.ResponseBuilder;
import com.springboot.util.http.ResultResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 博客文章 前端控制器
 * </p>
 *
 * @author zyj
 * @since 2023-03-23
 */
@RestController
@RequestMapping("/blogArticleView")
@Slf4j
@SpringBootRequestLog
@Tag(name = "博客展示")
@JwtToken(requestTokenType = RequestComm.REQUEST_TOKEN_TYPE_BLOGUSER, token=false)
public class BlogArticleViewController extends BaseController {

    @Resource
    private BlogArticleTagService blogArticleTagService;

    @Resource
    private BaseFileStoreFileUtil baseFileStoreFileUtil;

    @PostMapping(value = "/homeContent")
    @Operation(description = "获取博客主页内容", summary="获取博客主页内容")
    public ResultResponse<Page<HomeInfoVo>> homeContent(@RequestBody BlogHomeContentPageDto page) {
        try {
            page.setPreViewImg(baseFileStoreFileUtil.getDomain());
            Page<HomeInfoVo> homeContent = blogArticleTagService.homeContent(page);
            return ResponseBuilder.successData(homeContent);
        } catch (Exception e) {
            log.error("homeContent error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.GET_INFO_NULL.getValue(), RespErrorCode.GET_INFO_NULL.getDescripe());

    }

    @GetMapping(value = "/getSortList")
    @Operation(description = "获取博客分类列表")
    public ResultResponse<SortListVo> getSortList(@RequestParam(required = false) Integer id) {
        try {
            List<SortListVo> homeContent = blogArticleTagService.getSortList(id);
            return ResponseBuilder.successData(homeContent);
        } catch (Exception e) {
            log.error("getSortList error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.GET_INFO_NULL.getValue(), RespErrorCode.GET_INFO_NULL.getDescripe());

    }

    @GetMapping(value = "/getTagList")
    @Operation(description = "获取博客分类列表")
    public ResultResponse<TagListVo> getTagList(@RequestParam(required = false) Integer id) {
        try {
            List<TagListVo> homeContent = blogArticleTagService.getTagList(id);
            return ResponseBuilder.successData(homeContent);
        } catch (Exception e) {
            log.error("getTagList error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.GET_INFO_NULL.getValue(), RespErrorCode.GET_INFO_NULL.getDescripe());

    }

    @GetMapping(value = "/getContentInfo")
    @Operation(description = "获取博客内容")
    public ResultResponse<ArticleContentInfoVo> getContentInfo(Integer id) {
        try {
            ArticleContentInfoVo homeContent = blogArticleTagService.getContentInfo(id);
            return ResponseBuilder.successData(homeContent);
        } catch (Exception e) {
            log.error("getContentInfo error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.GET_INFO_NULL.getValue(), RespErrorCode.GET_INFO_NULL.getDescripe());

    }

    @ResponseBody
    @GetMapping(value = "/blogRss", produces = "application/rss+xml;charset=utf-8")
    public String blogRss() {
        try {
            String blogRss = blogArticleTagService.blogRss();
            return blogRss;
        } catch (Exception e) {
            log.error("blogRss error", e);
        }

        return null;
    }

    @GetMapping(value = "/getMeContentInfoView")
    @Operation(description = "获取关于我内容")
    public ResultResponse<BlogMeInfoViewVo> getMeContentInfo() {
        try {
            BlogMeInfoViewVo meContentInfo = blogArticleTagService.getMeContentViewInfo();
            return ResponseBuilder.successData(meContentInfo);
        } catch (Exception e) {
            log.error("getMeContentInfo error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.GET_INFO_NULL.getValue(), RespErrorCode.GET_INFO_NULL.getDescripe());

    }


}

